import java.nio.channels.spi.AbstractInterruptibleChannel;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Jiang Jinxi
 * Date: 2024-09-01
 * Time: 13:55
 */

public class Demo24 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1000);
        //生产者
        Thread t1 = new Thread(() -> {
            int i = 1;
            while(true){
                try {
                    queue.put(i);
                    System.out.println("t1生产: " + i);
                    i++;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        //消费者
        Thread t2 = new Thread(() -> {
            int elem = 0;
            while(true){
                try {
                    elem = queue.take();
                    System.out.println("t2消费: " + elem);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        t2.start();
    }
}
